package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EnvDataPrediction;
import com.ruoyi.system.service.IEnvDataPredictionService;

/**
 * 环境数据预测 控制器
 */
@RestController
@RequestMapping("/system/prediction")
public class PredictionController extends BaseController
{
    @Autowired
    private IEnvDataPredictionService envDataPredictionService;

    /**
     * 查询环境数据预测列表
     */
    @PreAuthorize("@ss.hasPermi('system:prediction:list')")
    @GetMapping("/list")
    public TableDataInfo list(EnvDataPrediction envDataPrediction)
    {
        startPage();
        List<EnvDataPrediction> list = envDataPredictionService.selectEnvDataPredictionList(envDataPrediction);
        return getDataTable(list);
    }

    /**
     * 获取环境数据预测详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:prediction:query')")
    @GetMapping(value = "/{predictionId}")
    public AjaxResult getInfo(@PathVariable("predictionId") Long predictionId)
    {
        return AjaxResult.success(envDataPredictionService.selectEnvDataPredictionById(predictionId));
    }

    /**
     * 删除环境数据预测
     */
    @PreAuthorize("@ss.hasPermi('system:prediction:remove')")
    @Log(title = "环境数据预测", businessType = BusinessType.DELETE)
    @DeleteMapping("/{predictionIds}")
    public AjaxResult remove(@PathVariable Long[] predictionIds)
    {
        return toAjax(envDataPredictionService.deleteEnvDataPredictionByIds(predictionIds));
    }
    
    /**
     * 生成空气质量预测数据
     */
    @PreAuthorize("@ss.hasPermi('system:prediction:generate')")
    @Log(title = "空气质量预测", businessType = BusinessType.INSERT)
    @GetMapping("/generate-air-quality")
    public AjaxResult generateAirQualityPrediction(Long pointId, String indicatorName, Integer days)
    {
        EnvDataPrediction prediction = envDataPredictionService.generateAirQualityPrediction(pointId, indicatorName, days);
        if (prediction != null) {
            return AjaxResult.success(prediction);
        } else {
            return AjaxResult.error("生成预测数据失败，请检查参数或历史数据");
        }
    }
    
    /**
     * 生成水质预测数据
     */
    @PreAuthorize("@ss.hasPermi('system:prediction:generate')")
    @Log(title = "水质预测", businessType = BusinessType.INSERT)
    @GetMapping("/generate-water-quality")
    public AjaxResult generateWaterQualityPrediction(Long pointId, String indicatorName, Integer days)
    {
        EnvDataPrediction prediction = envDataPredictionService.generateWaterQualityPrediction(pointId, indicatorName, days);
        if (prediction != null) {
            return AjaxResult.success(prediction);
        } else {
            return AjaxResult.error("生成预测数据失败，请检查参数或历史数据");
        }
    }
    
    /**
     * 生成噪声预测数据
     */
    @PreAuthorize("@ss.hasPermi('system:prediction:generate')")
    @Log(title = "噪声预测", businessType = BusinessType.INSERT)
    @GetMapping("/generate-noise")
    public AjaxResult generateNoisePrediction(Long pointId, Integer days)
    {
        EnvDataPrediction prediction = envDataPredictionService.generateNoisePrediction(pointId, days);
        if (prediction != null) {
            return AjaxResult.success(prediction);
        } else {
            return AjaxResult.error("生成预测数据失败，请检查参数或历史数据");
        }
    }
    
    /**
     * 获取预测趋势数据
     */
    @PreAuthorize("@ss.hasPermi('system:prediction:query')")
    @GetMapping("/trend-data/{predictionId}")
    public AjaxResult getPredictionTrendData(@PathVariable("predictionId") Long predictionId)
    {
        Map<String, Object> data = envDataPredictionService.getPredictionTrendData(predictionId);
        if (data != null) {
            return AjaxResult.success(data);
        } else {
            return AjaxResult.error("获取预测趋势数据失败");
        }
    }
    
    /**
     * 获取预测与实际对比数据
     */
    @PreAuthorize("@ss.hasPermi('system:prediction:query')")
    @GetMapping("/comparison-data/{predictionId}")
    public AjaxResult getPredictionComparisonData(@PathVariable("predictionId") Long predictionId)
    {
        Map<String, Object> data = envDataPredictionService.getPredictionComparisonData(predictionId);
        if (data != null) {
            return AjaxResult.success(data);
        } else {
            return AjaxResult.error("获取预测对比数据失败");
        }
    }
}